<!DOCTYPE HTML>
<html lang="ja-JP">
<head>
	<meta charset="utf-8" />
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="X-UA-Compatible" content="chrome=1" />
	
	<title>reiplayer/test</title>
	
	<script type="text/javascript" charset="utf-8" src="http://cdn.pixely.jp/shared/js/lib/scripty2/prototype.s2.min.js"></script>
	<script type="text/javascript" charset="utf-8" src="http://cdn.pixely.jp/shared/swf/jw/5.5/jwplayer.js"></script>
	<script type="text/javascript">
	
		var playerContainer      = 'player';
		var indexContainer       = 'index';
		var locationDetectionTag = 'h1';
		var titleElementID       = 'title';
		
		Event.observe(window, 'load', function()
		{
			if(window.location.hash){
				var hash = window.location.hash.replace('#', '');
				//hash = decodeURIComponent(hash);
				//hash = unescape(hash);
			
				if(hash.match(/^\?\/.+$/)){
					hash = hash.replace('?', '');
					actLoadIndex(hash.match(/^(.+\/).+\..+$/)[1]);
					setTimeout(function(){actLoadFile(hash);}, 1000);
				}
				else if(hash.match(/^\!\/.+$/)){
					hash = hash.replace('!', '');
					actLoadIndex(hash);
				}
				else{
					actLoadIndex('/');
				}
			}else{
				actLoadIndex('/');
			}
		});
		
		function actLoadIndex(pURI)
		{
			
			new Ajax.Updater(
				indexContainer,
				pURI,
				{
					'method'    : 'get',
					'onComplete' : function(){
						var loc = $$(locationDetectionTag)[0];
						var base = loc.innerHTML.replace('Index of ', '');
						window.location.hash = '!' + base;
			
						var as  = $$('div#' + indexContainer + ' * a');
						
						as.each(function(a){
							var href = a.getAttribute('href', 2);
							if(href.match(/^.+\/$/)) {
								a.href = 'javascript:void(0);';
								a.onclick = function(){
									$(indexContainer).update('loading...');
									actLoadIndex(base + href);
								}
							}
							if(href.match(/^.+\.(m4v|mp4|flv)$/)) {
								a.href = 'javascript:void(0);';
								a.onclick = function(){
									actLoadFile(base + href);
								}
							}
						});
					
					},
				}
			);
		}
		
		function actLoadFile(pURI)
		{
			window.location.hash = '?' + pURI;
			$(titleElementID).update('playing: ' + decodeURIComponent(pURI));
			
			//jwplayer(playerContainer).load(pURI);
			if(pURI.match(/^.+\.(m4v|mp4)$/)) jwplayer(playerContainer).setup({
				'id'        : 'video',
				'width'     : 640,
				'height'    : 360,
				'autostart' : true,
				'provider'  : 'http',
				'modes'     : [
					{
						'type':'html5',
						'config':{
							'file':pURI,
						},
					},
				],
			});
			
			if(pURI.match(/^.+\.(flv)$/)) jwplayer(playerContainer).setup({
				'id'        : 'video',
				'width'     : 640,
				'height'    : 360,
				'autostart' : true,
				'provider'  : 'video',
				'file'      : pURI,
				'modes'     : [
					{'type':'flash', src: 'http://cdn.pixely.jp/shared/swf/jw/5.5/player.swf'},
				],
			});
			
		}
		
	</script>
</head>
<body>
	<h3 id="title"></h3>
	<div id="player"></div>
	<div id="index"></div>
</body>
</html>